package com.fish.admin.module.salesman.manager;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fish.admin.module.salesman.domain.bo.SalesmanInfoBO;
import com.fish.admin.module.salesman.domain.dto.SalesmanInfoPageQueryDTO;
import com.fish.admin.module.salesman.domain.entity.SalesmanInfo;
import com.fish.admin.module.salesman.mapper.SalesmanInfoMapper;
import com.fish.admin.module.system.domain.entity.SysRole;
import com.fish.common.core.base.LoginAppUser;
import com.fish.common.core.context.UserContextManager;
import com.fish.common.core.enums.DataScopeEnum;
import com.fish.common.core.enums.EnabledEnum;
import com.fish.common.mybatisplus.util.PageUtils;
import com.fish.common.support.util.EnumUtils;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class SalesmanInfoManager {

    private final SalesmanInfoMapper salesmanInfoMapper;

    public IPage<SalesmanInfoBO> page(SalesmanInfoPageQueryDTO pageQuery) {
        MPJLambdaWrapper<SalesmanInfo> wrapper = new MPJLambdaWrapper<SalesmanInfo>();

        wrapper
                .selectAll(SalesmanInfo.class)
                .selectAs(SysRole::getName, SalesmanInfoBO::getRoleName)
                .selectAs(SysRole::getId, SalesmanInfoBO::getRoleId)
                .leftJoin(SysRole.class, on->on.eq(SalesmanInfo::getRoleId, SysRole::getId))
                .likeIfExists(SalesmanInfo::getSalesmanNo, pageQuery.getSalesmanNo())
                .likeIfExists(SalesmanInfo::getAccount, pageQuery.getAccount())
                .eqIfExists(SalesmanInfo::getGameQuota, pageQuery.getGameQuota())
                .eqIfExists(SalesmanInfo::getUsedGameQuota, pageQuery.getUsedGameQuota());
        LoginAppUser loginAppUser = UserContextManager.getRequiredContext();
        if (loginAppUser.isSalesman()) {
            Integer dataScope = Convert.toInt(loginAppUser.getInfos().get("dataScope"));
            if (EnumUtils.equals(dataScope, DataScopeEnum.ALL)) {
                wrapper.likeRight(SalesmanInfo::getDataPath, Convert.toStr(loginAppUser.getInfos().get("dataPath")));
            } else if (EnumUtils.equals(dataScope, DataScopeEnum.SELF_AND_CHILDREN)) {
                Long id = loginAppUser.getUserId();
                wrapper.eq(SalesmanInfo::getId, id).or().eq(SalesmanInfo::getParentId, id);
            }
        }

        return salesmanInfoMapper.selectJoinPage(PageUtils.build(pageQuery),  SalesmanInfoBO.class,wrapper);

    }

    public SalesmanInfo getById(Long id) {
        return salesmanInfoMapper.selectById(id);
    }

    public List<Long> listIdByParent(Long id) {
        MPJLambdaWrapper<SalesmanInfo> wrapper = new MPJLambdaWrapper<SalesmanInfo>();
        wrapper.select(SalesmanInfo::getId).eq(SalesmanInfo::getParentId,id).eq(SalesmanInfo::getEnabled, EnabledEnum.YES.getValue());
        return salesmanInfoMapper.selectJoinList(Long.class,wrapper);
    }

    public void save(SalesmanInfo salesmanInfo) {
        salesmanInfoMapper.insert(salesmanInfo);
    }
}
